package org.jpass.dc.business.project.logic;

import java.io.IOException;

import org.jpass.dc.business.project.bean.Clazz;
import org.jpass.dc.business.project.config.ConfigReader;
import org.jpass.dc.common.util.StringUtil;

public class ClassService {
	
	private FileService fileService = null;
	
	private ConfigReader classConfig = null;
	
	private String content; // class文件的内容
	
	public ClassService() {
		fileService = new FileService();
		classConfig = new ConfigReader("sys/resource/config/package.java.lang.xml");
	}
	
	/**
	 * 创建class文件
	 * @param clazz
	 * @throws IOException
	 */
	public void create(Clazz clazz) throws IOException {
		
		if(!fileService.isExist(clazz.getParentPath())) {
			fileService.createMultiFolder(clazz.getParentPath());
		}
		fileService.createFile(clazz.getPath());
		content = this.createContent(clazz);
		fileService.write(clazz.getPath(), content);
		clazz.setClassName(this.content);
	}
	
	public String getContent() {
		return this.content;
	}
	
	/**
	 * 构造clas文件的代码
	 * @param clazz
	 * @return
	 */
	private String createContent(Clazz clazz) {
		StringBuffer strbuff = new StringBuffer();
		String extend = "";
		String implement = "";
		if (!StringUtil.isBlank(clazz.getPackageName())) {
			strbuff.append("package ")
			.append(clazz.getPackageName())
			.append(";")
			.append("\n");
		}
		//String className = (String)classConfig.getElementValue("/package/class/class-name");
		//System.out.println(className);
		if (!StringUtil.isBlank(clazz.getSuperClassPackage()) && !"java.lang.Object".equals(clazz.getSuperClassPackage().trim())) {
			String c = clazz.getSuperClassPackage().substring(
					clazz.getSuperClassPackage().lastIndexOf("."), 
					clazz.getSuperClassPackage().length());
			strbuff.append("\n")
			.append("import ")
			.append(clazz.getSuperClassPackage())
			.append(";")
			.append("\n");
			extend = " extends " + c;
		}
		if (!StringUtil.isBlank(clazz.getImplementPackage())) {
			String packageStr = "";
			String[] packages = clazz.getPackageName().split(",");
			for (int i = 0, len = packages.length; i < len; i++) {
				strbuff.append("import ")
				.append(packages[i])
				.append("\n");
				packageStr = packageStr + packages[i].substring(
						packages[i].lastIndexOf(".", packages[i].length())) + ", ";
			}
			
			implement = " implements " + packageStr.substring(0, packageStr.length() - 2);
		}
		strbuff.append("\n")
		.append("public class ").append(clazz.getClassName()).append(extend).append(implement).append(" {")
		.append("\n");
		if (!StringUtil.isBlank(clazz.getMethod())) {
			String[] methods = clazz.getMethod().split(",");
			for (String m : methods) {
				if ("constructor".equals(m)) {
					strbuff.append("\n")
					.append("    public ").append(clazz.getClassName()).append("()").append(" {")
					.append("\n")
					.append("        super();")
					.append("\n")
					.append("    }");
				} else if ("main".equals(m)) {
					strbuff.append("\n")
					.append("    public static void main(String[] args) { ")
					.append("\n")
					.append("\n")
					.append("    }");
				}
			}
		}
		strbuff.append("\n")
		.append("}");
		
		return strbuff.toString();
	}
}
